package org.example.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.system.common.result.Result;
import org.example.system.mapper.BizTradeRecordMapper;
import org.example.system.model.entity.BizTradeRecord;
import org.example.system.model.form.TradeRecordForm;
import org.example.system.model.query.TradeRecordPageQuery;
import org.example.system.model.vo.PageVo;
import org.example.system.service.BizTradeRecordService;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Tag(name = "10.交易记录表")
@RestController
@RequestMapping("/api/v1/tradeRecord")
@Slf4j
public class TradeRecordController {

    @Resource
    private final BizTradeRecordService tradeRecordService;

    @Resource
    private final BizTradeRecordMapper tradeRecordMapper;

    @Resource
    private ModelMapper modelMapper;

    public TradeRecordController(BizTradeRecordService tradeRecordService, BizTradeRecordMapper tradeRecordMapper, ModelMapper modelMapper) {
        this.tradeRecordService = tradeRecordService;
        this.tradeRecordMapper = tradeRecordMapper;
        this.modelMapper = modelMapper;
    }


    /**
     * 多条件分页查询交易记录
     * @param tradeRecordPageQuery 查询条件
     * @return 交易记录分页信息
     * @author khb
     */
    @Operation(summary = "多条件分页查询交易记录")
    @PostMapping("/search")
    public Result<Object> queryTradeRecordByPage(@RequestBody TradeRecordPageQuery tradeRecordPageQuery) {
        PageVo<BizTradeRecord> pageVo = tradeRecordService.searchVoPage(tradeRecordPageQuery);
        return Result.success(pageVo);
    }

    /**
     * 根据交易记录id逻辑删除
     * @param id 交易记录id
     * @return 删除结果
     */
    @Operation(summary = "根据交易记录id逻辑删除")
    @PostMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        tradeRecordMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 新增一条交易记录
     * @param tradeRecordForm 交易记录信息
     * @return 新增结果
     */
    @Operation(summary = "新增一条交易记录")
    @PostMapping("/add")
    public Result<Object> addTradeRecord(@RequestBody TradeRecordForm tradeRecordForm){
        BizTradeRecord tradeRecord = modelMapper.map(tradeRecordForm, BizTradeRecord.class);
        tradeRecordService.save(tradeRecord);
        return Result.success();
    }

    /**
     * 根据id更新交易记录
     * @param tradeRecordForm 交易记录信息
     * @param id 交易记录id
     * @return 更新结果
     */
    @Operation(summary = "根据id更新交易记录")
    @PostMapping("/update/{id}")
    public Result<Object> updateTradeRecord(@RequestBody TradeRecordForm tradeRecordForm,@PathVariable Integer id){
        UpdateWrapper<BizTradeRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        BizTradeRecord tradeRecord = modelMapper.map(tradeRecordForm, BizTradeRecord.class);
        tradeRecord.setUpdateTime(LocalDateTime.now());
        tradeRecordService.update(tradeRecord, updateWrapper);
        return Result.success();
    }


    /**
     * 根据id修改订单状态
     * @param id 订单id
     * @param tradeStatus 订单状态
     * @return 修改结果
     */
    @Operation(summary = "根据id修改订单状态")
    @PostMapping("/updateStatus/{id}")
    public Result<Object> updateTradeStatus(@PathVariable Integer id, @RequestParam Integer tradeStatus) {
        UpdateWrapper<BizTradeRecord> updateWrapper = new UpdateWrapper<>();
        BizTradeRecord tradeRecord = new BizTradeRecord();
        updateWrapper.eq("id", id);
        updateWrapper.set("trade_status", tradeStatus);
        tradeRecordService.updateById(tradeRecord);
        return Result.success();
    }


    /**
     * 计算总营业额
     * @return 总营业额
     */
    @Operation(summary = "计算总营业额")
    @GetMapping("/getTotalTurnover")
    public Result<Object> sumTotalFee() {
        BizTradeRecord tradeRecord = new BizTradeRecord();
        QueryWrapper<BizTradeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(total_fee) as totalTurnover"); // totalTurnover为总营业额
        BizTradeRecord tr = tradeRecordService.getOne(queryWrapper);
        // 判空
        if (tr!= null) {
            tradeRecord.setTotalFee(tr.getTotalFee());
        }else {
            tradeRecord.setTotalFee(BigDecimal.valueOf(0));
        }
        return Result.success(tradeRecord);
    }

    /**
     * 计算指定年份的营业额
     * @param year 年份
     * @return 指定年份的营业额
     */
    @Operation(summary = "根据年份查询营业额")
    @GetMapping("/getAnnualTurnover")
    public BigDecimal getTotalTurnoverByYear(@RequestParam Integer year){
        return tradeRecordService.selectSumByYear(year);
    }

    /**
     * 根据指定的日期范围查询营业额
     * @param tradeRecordPageQuery 查询条件
     * @return 指定日期范围的营业额
     */
    @Operation(summary = "根据指定的日期范围查询营业额")
    @PostMapping("/getTurnoverByDateRange")
    public BigDecimal selectSumByDateRange (@RequestBody TradeRecordPageQuery tradeRecordPageQuery){
        return tradeRecordService.selectSumByDateRange(tradeRecordPageQuery);
    }
}
